/**
 * 检测人员负荷分析
 * @author yuan_rz
 * @date 2019/12/17 11:14
 */
define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            displayTable: false,
            dataList: [],
            form: {
                TIME: 'MONTH',
                AREA: ''
            },
            timeList: [{
                value: 'MONTH', label: '本月'
            }, {
                value: 'LAST_MONTH', label: '上月'
            }, {
                value: 'QUARTER', label: '本季度'
            }, {
                value: 'YEAR', label: '本年'
            }],
            chart: {
                nameList: [],
                timeList: [],
                sumList: [],
                percentList: []
            }
        },
        cols = [[
            {field: 'ROW_ID', width: 100, title: '排名'}
            , {field: 'PERSON', width: 100, title: '人员'}
            , {field: 'SERVICE_TIME', width: 100, title: '上门时数'}
            , {field: 'DOCNO_SUM', width: 100, title: '单数'}
        ]],
        moduleName = '检测人员负荷'
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'jcryfh',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'jcryfh_table'),
                    title: moduleName + '表',
                    id: 'jcryfh_table',
                    size: _layuiConfig.ui_size,
                    defaultToolbar: [],
                    height: 'full-400',
                    cols: cols,
                    data: vueData.dataList,
                    loading: false,
                    page: true,
                    done: function (res, curr, count) {

                    }
                });
            });
        },
        initChart: function () {
            var chartDiv = echarts.init(doc.getElementById('divChart'));

            var option = {
                toolbox: {
                    feature: {
                        dataView: {show: true, readOnly: false},
                        saveAsImage: {show: true}
                    }
                },
                title: {
                    x: 'center',
                    y: 'top',
                    text: '服务人员负荷'
                },
                grid: {
                    left: '3%',
                    right: '13%',
                    bottom: '3%',
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                },
                xAxis: [{
                    name: '上门时数/单数',
                    type: 'value'
                }],
                yAxis: [{
                    name: '姓名',
                    type: 'category',
                    data: vueData.chart.nameList
                }],
                series: [{
                    name: '上门时数/单数',
                    type: 'bar',
                    data: (function () {
                        var dataList = [];
                        for (var i = 0; i < vueData.chart.percentList.length; i++) {
                            dataList.push(vueData.chart.percentList[i].value);
                        }
                        return dataList;
                    })(),
                    label: {
                        normal: {
                            formatter: function (params) {
                                for (var i = 0; i < vueData.chart.percentList.length; i++) {
                                    if (params.value == vueData.chart.percentList[i].value) {
                                        return vueData.chart.percentList[i].label;
                                    }
                                }
                            },
                            show: true,
                        }
                    }
                }]
            };
            chartDiv.setOption(option);
        },
        reloadTable: function () {
            app.ajax({
                xurl: 'bb/jcryfh/getListData',
                xdata: vueData.form,
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        vueData.dataList = result.data.data;
                        var nameList = [];
                        var timeList = [];
                        var sumList = [];
                        var percentList = [];
                        for (var i = vueData.dataList.length; i > 0; i--) {
                            var data = vueData.dataList[i - 1];
                            data.ROW_ID = i;
                            nameList.push(data['PERSON']);
                            timeList.push(data['SERVICE_TIME']);
                            sumList.push(data['DOCNO_SUM']);
                            percentList.push({
                                label: data['SERVICE_TIME'] + '/' + data['DOCNO_SUM'],
                                value: (data['SERVICE_TIME'] / data['DOCNO_SUM']).toFixed(1)
                            });
                        }
                        vueData.chart.nameList.pushArray(nameList, true);
                        vueData.chart.timeList.pushArray(timeList, true);
                        vueData.chart.sumList.pushArray(sumList, true);
                        vueData.chart.percentList.pushArray(percentList, true);
                        self._reloadTable({id: 'jcryfh_table', data: vueData.dataList});
                        self.initChart();
                    }
                }
            });
        },
        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    watch: {
                        'form.TIME': function (newValue, oldValue) {
                            self.reloadTable();
                        },
                        'form.AREA': function (newValue, oldValue) {
                            self.reloadTable();
                        }
                    },
                    methods: {
                        openAreaWindow: function () {
                            var dataList = [];
                            var itemList = vueData.form.AREA.split(',');
                            for (var i = 0; i < itemList.length; i++) {
                                if(itemList[i] != '' && itemList[i] != null){
                                    dataList.push({GWAD002: itemList[i]});
                                }
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        columnList: [{label: '区域编号', prop: 'GWAD002', width: '150'},
                                            {label: '区域名称', prop: 'GWADL004', width: '150'},
                                            {label: '助记码', prop: 'GWADL005', width: '150'}],
                                        url: 'qry/getDataList',
                                        searchParam: {
                                            method: 'q_gwad002'
                                        },
                                        dataList: dataList,
                                        key: 'GWAD002'
                                    },
                                    cb: function (data) {
                                        var list = [];
                                        for (var i = 0; i < data.length; i++) {
                                            list.push(data[i].GWAD002);
                                        }
                                        vueData.form.AREA = list.join(',');
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择区域',
                                    area: ['600px', '510px']
                                }
                            });
                        },
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            setTimeout(function(){
                self.reloadTable();
            }, 500);
        }
    });
});